﻿//因为跟进记录和钥匙记录很像,所以两个页面用同一个js
var LeftNavPropertyFollow = {};

$(function () {

    //钥匙的部门提示
    EmployeePrompt.getDepartsByInput("txtDeptNames", "请输入部门名称");
    //钥匙的部门提示end

    dataBind.bindOption(29, "#selPropertyFollowType");

    //判断url
    var url = window.location.href;
    if (url.indexOf("follow") >= 0) {
        //房源状态
        propertyStatusMultipleCheckBox("#txtPropertyStatus", "propertyStatusSelect", PropertyStatusFilter.PropertyStatus, ['propertyType', 'roomType', 'hidBuilding']);

        //初始化EstateList的属性或方法Start  
        LeftNavPropertyFollow.getEstateUrl = function () {
            var estateName = $('#txtEstateNames').val();
            return '/property/estates-for-region-keyids-obtain-ajax?EstateName=' + escape(estateName);
        };
        //初始化EstateList的属性或方法End
        
        //初始化build的属性或方法Start  
        LeftNavPropertyFollow.getbuildUrl = function () {
            return '/property/building-for-estate-obatin-ajax?EstateName=' + escape($("#txtEstateNames").val()) + '&BuildName=' + $("#btnBuilding").val();
        };
        //初始化build的属性或方法End

        //楼盘自动完成及赋值隐藏域
        $("#txtEstateNames").autocomplete(LeftNavPropertyFollow.getEstateUrl, {
            width: 260,
            max: 1000,
            Default: 1,
            selectFirst: false,
            multipleSeparator: '+',
            formatItem: function (row) {
                return row.EstateFullName;
            },
            formatResult: function (row) {
                return row.EstateName;
            }
        }).result(function (a, b) {
            $("#hidEstateKeyId").val(b.KeyId);
            $("#txtHouseNo").removeAttr("readonly");
            $("#btnBuilding").removeAttr("readonly");
        });

        $('#txtEstateNames').bind('change', function () {
            $('#btnBuilding').val('');
            $('#hidBuilding ul').html('');
            $(this).attr('isexistbuild', 'true');
            $("#hidEstateKeyId").val('');
            if ($(this).val() == "") {
                $("#txtHouseNo").attr("readonly", "readonly").val("");
                $("#btnBuilding").attr("readonly", "readonly").val("");
            }
        });

        //栋座自动完成及赋值隐藏域
        $("#btnBuilding").autocomplete(LeftNavPropertyFollow.getbuildUrl, {
            width: 260,
            max: 1000,
            Default: 1,
            selectFirst: false,
            cacheLength: 0,
            multiple: true,
            clickMode: true,
            minChars: 0,
            formatItem: function (row) {
                return row.BuildingName;
            },
            formatResult: function (row) {

                return row.BuildingName;
            }
        }).result(function (a, b) {
            $("#hidBuildKeyId").val(b.KeyId);
        });
        $("#btnBuilding").bind('change', function () {
            $("#hidBuildKeyId").val('');
        });

        //根据楼盘名获得栋座单元
        LeftNavPropertyFollow.getBuildingByEstate = function (ele, txtEstateId) {
            var tempEstateValue = $('#' + txtEstateId).val();
            var tempEstateElement = $('#' + txtEstateId);
            var reg = /[+]/g;
            if (!tempEstateValue || reg.test(tempEstateValue) || tempEstateElement.attr('isexistbuild') == 'false') {
                $("#btnBuilding").val("");
                $("#hidBuilding").css('display', 'none');
                $("#hidBuilding ul").html("");
                return false;
            }
            var reqData = {
                'EstateName': tempEstateValue
            };
            $.AjaxAgency('/property/building-for-estate-obatin-ajax', reqData, function (data) {
                if (data) {
                    tempEstateElement.attr('isexistbuild', 'false');
                    multipleBuildingCheckBox('#btnBuilding', 'hidBuilding', data, ['propertyStatusSelect', 'propertyType', 'roomType']);
                }
            });
        };
    } else {
        //城区数据加载
        multipleRegionsCheckBox('#txtDistrict', 'hidDistrict', 1, false, ['propertyStatusSelect', 'propertyType', 'roomType', 'hidBuilding'], '城区');
        //片区数据加载
        dataBind.bindOption(29, "#selPropertyFollowType");
        //房源状态
        propertyStatusMultipleCheckBox("#txtPropertyStatus", "propertyStatusSelect", PropertyStatusFilter.PropertyStatus, ['propertyType', 'hidDistrict', 'hidArea', 'roomType', 'hidBuilding']);

        //片区数据Start
        $('#hidDistrict :checkbox[name=chbhidDistrict]').click(function () {
            var districtKeyIds = [];
            $('#hidDistrict :checkbox[name=chbhidDistrict]:checked').each(function () {
                districtKeyIds.push($(this).val());
            });
            if (districtKeyIds.length <= 0) {
                $('#txtArea').val('片区');
            }

            multipleRegionsCheckBox('#txtArea', 'hidArea', 2, districtKeyIds, ['propertyStatusSelect', 'roomType', 'propertyType', 'hidDistrict'], '片区');//片区数据加载

        });
        //片区数据End

        //初始化EstateList的属性或方法Start  
        LeftNavPropertyFollow.getEstateUrl = function () {
            var districtKeyIds = '', areaKeyIds = '', estateName = [];
            $('#hidDistrict input[type=checkbox]:checked').each(function () {
                districtKeyIds += $(this).val() + ',';
            });
            $('#hidArea input[type=checkbox]:checked').each(function () {
                areaKeyIds += $(this).val() + ',';
            });
            estateName = $('#txtEstateNames').val().split('+');
            return '/property/estates-for-region-keyids-obtain-ajax?DistrictKeyIds=' + districtKeyIds.substr(0, districtKeyIds.length - 1) + '&AreaKeyIds=' + areaKeyIds.substr(0, districtKeyIds.length - 1) + '&EstateName=' + escape(estateName[estateName.length - 1]);
        };
        //初始化EstateList的属性或方法End

        //初始化build的属性或方法Start  
        LeftNavPropertyFollow.getbuildUrl = function () {
            return '/property/building-for-estate-obatin-ajax?EstateName=' + escape($("#txtEstateNames").val()) + '&BuildName=' + $("#btnBuilding").val();
        };
        //初始化build的属性或方法End

        //楼盘自动完成及赋值隐藏域
        $("#txtEstateNames").autocomplete(LeftNavPropertyFollow.getEstateUrl, {
            width: 260,
            max: 1000,
            Default: 1,
            selectFirst: false,
            cacheLength: 0,
            multiple: true,
            multipleSeparator: '+',
            formatItem: function (row) {
                return row.EstateFullName;
            },
            formatResult: function (row) {
                return row.EstateName;
            }
        }).result(function (a, b) {
            $("#hidEstateKeyId").val(b.KeyId);
            $("#txtHouseNo").removeAttr("readonly");
            $("#btnBuilding").removeAttr("readonly");
        });

        $('#txtEstateNames').bind('change', function () {
            $('#btnBuilding').val('');
            $('#hidBuilding ul').html('');
            $(this).attr('isexistbuild', 'true');
            if ($(this).val() == "") {
                $("#txtHouseNo").attr("readonly", "readonly").val("");
                $("#btnBuilding").attr("readonly", "readonly").val("");
            }
        });

        //栋座自动完成及赋值隐藏域
        $("#btnBuilding").autocomplete(LeftNavPropertyFollow.getbuildUrl, {
            width: 260,
            max: 1000,
            Default: 1,
            selectFirst: false,
            cacheLength: 0,
            multiple: true,
            clickMode: true,
            minChars: 0,
            formatItem: function (row) {
                return row.BuildingName;
            },
            formatResult: function (row) {

                return row.BuildingName;
            }
        }).result(function (a, b) {
            $("#hidBuildKeyId").val(b.KeyId);
        });
        $("#btnBuilding").bind('change', function () {
            $("#hidBuildKeyId").val('');
        });

        //根据楼盘名获得栋座单元
        LeftNavPropertyFollow.getBuildingByEstate = function (ele, txtEstateId) {
            var tempEstateValue = $('#' + txtEstateId).val();
            var tempEstateElement = $('#' + txtEstateId);
            var reg = /[+]/g;
            if (!tempEstateValue || reg.test(tempEstateValue) || tempEstateElement.attr('isexistbuild') == 'false') {
                $("#btnBuilding").val("");
                $("#hidBuilding").css('display', 'none');
                $("#hidBuilding ul").html("");
                return false;
            }
            var reqData = {
                'EstateName': tempEstateValue
            };
            $.AjaxAgency('/property/building-for-estate-obatin-ajax', reqData, function (data) {
                if (data) {
                    tempEstateElement.attr('isexistbuild', 'false');
                    multipleBuildingCheckBox('#btnBuilding', 'hidBuilding', data, ['propertyStatusSelect', 'hidDistrict', 'hidArea', 'propertyType', 'roomType']);
                }
            });
        };
    }

    //盘源归属智能提示Start
    EmployeePrompt.getDepartAndEmployeeByInput('txtDepartmentName', 'hidDepartmentId', 'txtEmployeeName', 'hidUserId', false, 'private', 1, '业务员姓名', '请输入部门名称', true);
    //盘源归属智能提示End

    //钥匙回收
    $("#btnRecover").click(function () {
        var currentkey = $("#dataTable input[type=radio]:checked");
        if (currentkey.length < 1) {
            $.OKDialogAgency('<div class="tc">请先选择钥匙!</div>');
            return;
        }
        if (!ListAndDetailRight.CheckRightRol(currentkey, "Property.Key.Recover", "回收权限")) {
            return false;
        }
        var currentkeyId = currentkey.attr("keyid");
        $.AjaxAgency('/property/property-key-reclaim-ajax', { 'keyId': currentkeyId }, function (data) {
            if (data == 'ok') {
                $.OKDialogAgency('<div class="tc">操作成功!</div>', '提示', function () {
                    $("#btnSubmit").click();
                });
            } else {
                $.OKDialogAgency('<div class="tc">' + data + '</div>');
            }
        });
    });
    //钥匙归还
    $('#btnReturn').click(function () {
        var currentkey = $("#dataTable input[type=radio]:checked");
        if (currentkey.length < 1) {
            $.OKDialogAgency('<div class="tc">请先选择钥匙!</div>');
            return;
        }
        
        var status = currentkey.attr("status");
        if (status != 2) {
            $.OKDialogAgency('<div class="tc">必须选择已借出的钥匙!</div>');
            return;
        }
        if (!ListAndDetailRight.CheckRightRol(currentkey, "Property.Key.Return", "还钥权限")) {
            return false;
        }
        var currentkeyId = currentkey.attr("propertyKeyBorrowKeyId");

        $.AjaxAgency('/property/property-key-return-ajax', { 'keyId': currentkeyId }, function (data) {
            if (data == 'ok') {
                $.OKDialogAgency('<div class="tc">还钥成功!</div>', '提示', function () {
                    $("#btnSubmit").click();
                });
            } else {
                $.OKDialogAgency('<div class="tc">' + data + '</div>');
            }
        });
    });
    //清空事件
    $('#btnClear').click(function () {
        $("#hidEstateKeyId").val('');
        $("#hidBuilding ul").html('');
        $("#propertyStatusSelect input[type=checkbox]").attr("checked", false);
        $('#txtDepartmentName').css('color', 'gray');
        $("#hidDepartmentId").val('');
        $('#txtEmployeeName').css('color', 'gray');
        $("#hidUserId").val('');
        $("#txtHouseNo").attr("readonly", "readonly");
        $("#hidAuditPersonKeyId").val('');
        $("#hidAuditPersonDeptKeyId").val('');
        $("#hidBuildKeyId").val('');
        
    });

    //审核人文本框清空同时清空隐藏文本框的值
    $('#txtAuditPersonName').bind('change', function () {
        $("#hidAuditPersonKeyId").val('');
        $("#hidAuditPersonDeptKeyId").val('');
    });

    $("#delfollow").click(function () {
        var follows = $("#dataTable input[name=chbkid]:checked");
        var roleRight = $("#hidPermissions").val();
        var myDepartments = $("#hidMyDepartments").val();
        var operator = $("#hidOperatorID").val();
        ids = "";
        follows.each(function () {
            ids += ($(this).val() + ',');
        });
        if (!ids) {
            $.OKDialogAgency('<div class="tc">请选择跟进!</div>');
            return;
        }
        if (PropertyFollowRight.CheckDelRight(follows, roleRight, myDepartments, operator)) {
            $.ajax({
                type: 'post',
                url: '/Property/property-follow-batch-remove-ajax',
                data: { 'keyids': ids },
                success: function (data) {
                    if (data == "OK") {
                        $.OKDialogAgency('<div class="tc">删除成功!</div>', '提示', function () {
                            window.location.href = window.location.href;
                        });
                    }
                    else {
                        $.OKDialogAgency('<div class="tc">删除出错!</div>');
                    }
                }
            });
        }
    });
    //浮动的工具栏 yjj添加
    var state = true; //存储顶部显示的状态
    $(window).scroll(function () {
        if ($(window).scrollTop() > $("#ajaxPages").offset().top) {
            if (state) {
                //执行浮动层出现后的代码
                $(".fixed-div").show();
                $("#gdWarZoneList .thead").css("visibility", "hidden");
                $("#gdWarZoneList .last").css("width", "150px");
                $("#editToolsBox").attr("class", "float-edit-toos");
            }
            state = false;
        } else {
            if (!state) {
                //执行浮动层出现前的代码
                $(".fixed-div").hide();
                $("#gdWarZoneList .thead").css("visibility", "visible");
                $("#gdWarZoneList .last").css("width", "");
                $("#editToolsBox").removeAttr("class");
            }
            state = true;
        }
        $(".tab-panel-wrapper").scroll(function () {
            var l = $(".tab-panel-wrapper").scrollLeft();
            $(".fixed-div div").css("margin-left", "-" + l + "px");
        });
    });
});